/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.tmall.remote.RemoteTmallServiceImpl
   Module Description   :

   Date Created      : 2011/10/17
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.tmall.remote;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.tmall.service.TmallService;

/**
 * @author feeling.wang
 * 
 */
public class RemoteTmallServiceImpl implements RemoteTmallService {
	@Autowired
	private TmallService tmallService;

	private boolean debug = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.tmall.remote.RemoteTmallService#getTrade(java.lang.String)
	 */
	@Override
	public String getTrade(String tid) {
		try {
			tmallService.getTrade(tid);
			return "true";
		} catch (Exception e) {
			return "exception:" + e.getMessage();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.tmall.remote.RemoteTmallService#getTrades(java.util.Date, java.util.Date, java.lang.String,
	 * int)
	 */
	@Override
	public String getTrades(Date start, Date end, String status, int page) {
		try {
			if (!isDebug()) {
				// 非 debug 才執行
				tmallService.getTrades(start, end, status, page);
			} else {
				// by pass
			}
			return "true";
		} catch (Exception e) {
			return "exception:" + e.getMessage();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.tmall.remote.RemoteTmallService#tradesMemoAdd(java.util.Date, java.util.Date)
	 */
	@Override
	public String tradesMemoAdd(Date start, Date end) {
		try {
			tmallService.tradesMemoAdd(start, end);
			return "true";
		} catch (Exception e) {
			return "exception:" + e.getMessage();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.tmall.remote.RemoteTmallService#tradesSoldIncrementGet(java.util.Date, java.util.Date,
	 * java.lang.String)
	 */
	@Override
	public String tradesSoldIncrementGet(Date start, Date end, String status) {
		try {
			if (!isDebug()) {
				// 非 debug 才執行
				int page = 10;
				int pageSize = 50;
				for (int i = page; i > 0; i--) {
					tmallService.tradesSoldIncrementGet(start, end, status, null, i, pageSize);
				}
			} else {
				// by pass
			}
			return "true";
		} catch (Exception e) {
			return "exception:" + e.getMessage();
		}
	}

	@Override
	public boolean isDebug() {
		if ("www.yaodian100.com".equals(ServerValue.getFrontHostname())) {
			return false;
		} else {
			// sit, dev 如果 debug 沒有改為 false (預設 true), 不執行
			return debug;
		}
	}

	@Override
	public boolean setDebug(boolean debugFlag) {
		debug = debugFlag;
		return debug;
	}
}
